<?php
error_reporting(E_ALL ^ E_NOTICE);

require_once('Rest.inc.php');

class API extends REST {	
	const DB_SERVER = 'mysql1.alwaysdata.com';
	const DB_USER = '81625';
	const DB_PASSWORD = 'nuitinfo-ensibs-2013';
	const DB = 'nuitinfo-ensibs-2013_sql';
	
	private $db = NULL;

	public function __construct(){
		parent::__construct();
		$this->dbConnect();
	}
	
	private function dbConnect(){
		$this->db = new PDO('mysql:host='.self::DB_SERVER.';dbname='.self::DB.';charset=utf8', self::DB_USER, self::DB_PASSWORD);
	}
	
	/*
	 * Public method for access api.
	 * This method dynmically call the method based on the query string
	 *
	 */
	public function processApi(){
		$func = strtolower(trim(str_replace('/','',$_REQUEST['query'])));
		if((int)method_exists($this,$func) > 0)
			$this->$func();
		else{
			$error = array('status' => 'Failed', 'msg' => 'Method not found');
			$this->response($this->json($error),404);				
		}
			
	}
	
	private function search(){	

		$product = $this->_request['product'];
		$category = $this->_request['category'];
		
		$true_product = isset($product) && !empty($product);
		$true_category = isset($category) && !empty($category);
		
		if($true_product && !$true_category) {
			$stmt = $this->db->prepare('SELECT * FROM produits WHERE nom LIKE ?;');
			$stmt->execute(array('%'.$product.'%'));
		} else if($true_category && !$true_product) {
			$stmt = $this->db->prepare('SELECT produits.id, produits.nom, prix, vendeur, produits.image, description FROM produits, categories, assocProduitCategorie WHERE produits.id = assocProduitCategorie.idProduit AND assocProduitCategorie.idCategorie = categories.id AND categories.nom = ?;');
			$stmt->execute(array($category));
		} else if ($true_category && $true_product) {
			$stmt = $this->db->prepare('SELECT produits.id, produits.nom, prix, vendeur, produits.image, description, lien FROM produits, categories, assocProduitCategorie WHERE produits.id = assocProduitCategorie.idProduit AND assocProduitCategorie.idCategorie = categories.id AND categories.nom = ? AND produits.nom LIKE ?;');
			$stmt->execute(array($category, '%'.$product.'%'));
		} else {
			$stmt = $this->db->prepare('SELECT * FROM produits;');
			$stmt->execute();
		}
		
		$all = [];
		while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
			$stmt2 = $this->db->prepare('SELECT categories.nom FROM categories, assocProduitCategorie WHERE assocProduitCategorie.idProduit = ? AND assocProduitCategorie.idCategorie = categories.id;');
			$stmt2->execute(array($data['id']));
			$categories = $stmt2->fetchAll(PDO::FETCH_ASSOC);
			$data['categories'] = $categories;
			$all[] = $data;
		}
		
		$this->response($this->json($all), 200);
	}
	
	private function category() {
	
		$stmt = $this->db->prepare('SELECT * FROM categories;');
		$stmt->execute();
		$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$this->response($this->json($data), 200);
	}
	
	private function topic() {
	
		$stmt = $this->db->prepare('SELECT * FROM topic;');
		$stmt->execute();
		$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$this->response($this->json($data), 200);
	}
	
	private function creerTopic() {
	
		$title = $this->_request['title'];
		$content = $this->_request['content'];
		
		$stmt = $this->db->prepare('INSERT INTO topic(user_id,title,content,weight,date,statut) values(1,?,?,0,NOW(),0);');
		$stmt->execute(array($title,$content));
		
		$this->response('', 200);
	}
	
	private function addTagTopic() {
	
		$idTopic = $this->_request['idTopic'];
		$idTag = $this->_request['idTag'];
	
		$stmt = $this->db->prepare('INSERT INTO topic_tag(topic_id,tag_id) values(?,?);');
		$stmt->execute(array($idTopic,$idTag));
		$this->response('', 200);
	}
	
	private function tags() {
	
		$stmt = $this->db->prepare('SELECT * FROM tag;');
		$stmt->execute();
		$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$this->response($this->json($data), 200);
	}
	
	
	private function cloturerTopic() {
	
		$topicId = $this->_request['id'];
	
		$stmt = $this->db->prepare('UPDATE topic set statut = 1 where id = ?;');
		$stmt->execute(array($topicId));
		$this->response('', 200);
	}
	
	private function addReply() {
		$idTopic = $this->_request['id'];
		$content = $this->_request['content'];
		
		$stmt = $this->db->prepare('INSERT INTO reply(topic_id,user_id,content,weight,date) values(?,1,?,0,NOW());');
		$stmt->execute(array($idTopic,$content));
		$this->response('', 200);
	}
	
	private function getReply() {
		$idTopic = $this->_request['id'];
		
		$stmt = $this->db->prepare('SELECT * FROM reply where topic_id = ?;');
		$stmt->execute(array($idTopic));
		$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$this->response($this->json($data), 200);
	}

	/*
	 *	Encode array into JSON
	*/
	private function json($data){
		if(is_array($data)){
			return json_encode($data);
		}
	}
}

$api = new API;
$api->processApi();
?>